package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.RoleDepartmentDTO;
import com.mrdeer.common.entity.sys.RoleDepartment;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.RoleDepartmentMapper;
import com.mrdeer.web.service.sys.SysRoleDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysRoleDepartmentServiceImpl extends ServiceImpl<RoleDepartmentMapper, RoleDepartment> implements SysRoleDepartmentService {

    @Resource
    private RoleDepartmentMapper roleDepartmentMapper;

    @Override
    public List<RoleDepartment> findByRoleId(String roleId) {
        RoleDepartmentDTO dto = new RoleDepartmentDTO();
        dto.setRoleId(roleId);
        return roleDepartmentMapper.selectList(fuzzyQuery(dto));
    }

    @Override
    public void deleteByRoleId(String roleId) {
        delete(roleId);
    }

    @Override
    public void deleteByDepartmentId(String departmentId) {
        RoleDepartmentDTO dto = new RoleDepartmentDTO();
        dto.setDepartmentId(departmentId);
        roleDepartmentMapper.delete(fuzzyQuery(dto));
    }

    @Override
    public RoleDepartment add(RoleDepartment roleDepartment) {
        if (roleDepartmentMapper.insert(roleDepartment) > 0) {
            return roleDepartmentMapper.selectById(roleDepartment.getId());
        }
        throw new BusinessException("add RoleDepartment failed");
    }

    @Override
    public boolean delete(String id) {
        return roleDepartmentMapper.deleteById(id) > 0;
    }

    @Override
    public RoleDepartment update(RoleDepartment roleDepartment) {
        if (roleDepartmentMapper.updateById(roleDepartment) > 0) {
            RoleDepartment rd = roleDepartmentMapper.selectById(roleDepartment.getId());
            return rd;
        }
        throw new BusinessException("update failed");
    }

    @Override
    public RoleDepartment getById(String id) {
        return roleDepartmentMapper.selectById(id);
    }

    @Override
    public Page<RoleDepartment> list(RoleDepartmentDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return roleDepartmentMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<RoleDepartment> fuzzyQuery(RoleDepartmentDTO dto) {
        LambdaQueryWrapper<RoleDepartment> queryWrapper = Wrappers.<RoleDepartment>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getDepartmentId())) {
                queryWrapper.eq(RoleDepartment::getDepartmentId, dto.getDepartmentId());
            }
            if (StrUtil.isNotBlank(dto.getRoleId())) {
                queryWrapper.eq(RoleDepartment::getRoleId, dto.getRoleId());
            }
        }
        return queryWrapper;
    }
}
